<?php

/**
 * Capital Direct Lending (http://www.philbsd.com/cdl)
 *
 * Copyright (C) 2008 Philippine Business Solution Developers <http://www.philbsd.com>
 * All rights reserved.
 *
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License v3 license
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@philbsd.com so we can send you a copy immediately.
 *
 * @copyright  (c) 2008 Philippine Business Solution Developers. (http://www.philbsd.com)
 * @license    http://www.gnu.org/licenses/gpl-3.0.txt
 * @version    $Id: loans.php 161 2008-04-13 07:25:10Z artiskool $
 */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Loans extends Model
{
	public function getAll()
	{
		$ret = array();
		// get all entries from loans table (it can be limit down to number of records to retrieve)
		$this->db->select('*')->from('loans');
		if ($loanQuery = $this->db->get()) {
			foreach ($loanQuery->result_array() as $row) {
				// get borrower information
				$this->db->select('*')->from('borrowers')
						 ->where('id', $row['borrower_id']);
				if ($borrowerQuery = $this->db->get()) {
					$borrower = $borrowerQuery->row_array();
					// get profile information
					$this->db->select('*')->from('profiles')
						 ->where('id', $borrower['profile_id']);
					if ($profileQuery = $this->db->get()) {
						$profile = $profileQuery->row_array();
						$borrower['Profile'] = $profile;
					}
					$row['Borrower'] = $borrower;
				}
				// get co-borrower information
				$this->db->select('*')->from('borrowers')
						 ->where('id', $row['coborrower_id']);
				if ($coBorrowerQuery = $this->db->get()) {
					$coBorrower = $coBorrowerQuery->row_array();
					// get profile information
					$this->db->select('*')->from('profiles')
						 ->where('id', $coBorrower['profile_id']);
					if ($profileQuery = $this->db->get()) {
						$profile = $profileQuery->row_array();
						$coBorrower['Profile'] = $profile;
					}
					$row['CoBorrower'] = $coBorrower;
				}
				$ret[] = $row;
			}
		}
		return $ret;
	}

	public function getLoan($id)
	{
		$ret = array();
		// get all entries from loans table (it can be limit down to number of records to retrieve)
		$this->db->select('*')->from('loans')
				 ->where('id', $id);
		if ($loanQuery = $this->db->get()) {
			$loan = $loanQuery->row_array();
			$this->db->select('*')->from('borrowers')
					 ->where('id', $loan['borrower_id']);
			if ($borrowerQuery = $this->db->get()) {
				$borrower = $borrowerQuery->row_array();
				// get profile information
				$this->db->select('*')->from('profiles')
					 ->where('id', $borrower['profile_id']);
				if ($profileQuery = $this->db->get()) {
					$profile = $profileQuery->row_array();
					$borrower['Profile'] = $profile;
				}
				$loan['Borrower'] = $borrower;
			}
			// get co-borrower information
			$this->db->select('*')->from('borrowers')
					 ->where('id', $loan['coborrower_id']);
			if ($coBorrowerQuery = $this->db->get()) {
				$coBorrower = $coBorrowerQuery->row_array();
				// get profile information
				$this->db->select('*')->from('profiles')
					 ->where('id', $coBorrower['profile_id']);
				if ($profileQuery = $this->db->get()) {
					$profile = $profileQuery->row_array();
					$coBorrower['Profile'] = $profile;
				}
				$loan['CoBorrower'] = $coBorrower;
			}

            $loan['total_paid'] = 0;
            // get payments information
			$this->db->select('*')->from('payments')
					 ->where('loan_id', $id);
            if ($paymentQuery = $this->db->get()) {
                $ctr = 0;
                foreach ($paymentQuery->result_array() as $row) {
                    $loan['Payment'][] = $row;
                    $loan['total_paid'] += $loan['Payment'][$ctr]['amount_paid'];
                    $ctr++;
                }
            }

			$ret = $loan;
		}
		return $ret;
	}
    public function getPayment($id)
	{
		$ret = array();
		// get all entries from payment table (it can be limit down to number of records to retrieve)
		$this->db->select('*')->from('payments')
				 ->where('id', $id);
		if ($paymentQuery = $this->db->get()) {
			$payment = $paymentQuery->row_array();

            // get all entries from loans table (it can be limit down to number of records to retrieve)
		    $this->db->select('*')->from('loans')
				 ->where('id', $payment['loan_id']);
		    if ($loanQuery = $this->db->get()) {
			   $loan = $loanQuery->row_array();
			   $this->db->select('*')->from('borrowers')
					 ->where('id', $loan['borrower_id']);
			   if ($borrowerQuery = $this->db->get()) {
				  $borrower = $borrowerQuery->row_array();
				  // get profile information
				  $this->db->select('*')->from('profiles')
					 ->where('id', $borrower['profile_id']);
				  if ($profileQuery = $this->db->get()) {
				  	$profile = $profileQuery->row_array();
					$borrower['Profile'] = $profile;
				  }
               }

				$payment['Borrower'] = $borrower;
			}



        	$ret = $payment;
        }
		return $ret;
	}
    public function getBorrower($id)
	{
		$ret = array();
        // get all entries from loans table (it can be limit down to number of records to retrieve)
		$this->db->select('borrower_id,id')->from('loans')
				 ->where('id', $id);
		if ($loanQuery = $this->db->get()) {
			$loan = $loanQuery->row_array();

			// get all entries from borrowers table (it can be limit down to number of records to retrieve)
			$this->db->select('*')->from('borrowers')
				 ->where('id', $loan['borrower_id']);
			if ($borrowerQuery = $this->db->get()) {
				$borrower = $borrowerQuery->row_array();

            	// get all entries from Businesses table (it can be limit down to number of records to retrieve)
		    	$this->db->select('*')->from('profiles')
					 ->where('id', $borrower['profile_id']);
		    	if ($profileQuery = $this->db->get()) {
			   		$profile = $profileQuery->row_array();
			   		$borrower['Profile'] = $profile;
				}
				$loan['Borrower'] = $borrower;
        	}

        	$ret = $loan;
		}
		return $ret;
	}

    public function getCoBorrower($id)
	{
		$ret = array();
        // get all entries from loans table (it can be limit down to number of records to retrieve)
		$this->db->select('coborrower_id,id')->from('loans')
				 ->where('id', $id);
		if ($loanQuery = $this->db->get()) {
			$loan = $loanQuery->row_array();

			// get all entries from borrowers table (it can be limit down to number of records to retrieve)
			$this->db->select('*')->from('borrowers')
				 ->where('id', $loan['coborrower_id']);
			if ($borrowerQuery = $this->db->get()) {
				$borrower = $borrowerQuery->row_array();

            	// get all entries from Profile table (it can be limit down to number of records to retrieve)
		    	$this->db->select('*')->from('profiles')
					 ->where('id', $borrower['profile_id']);
		    	if ($profileQuery = $this->db->get()) {
			   		$profile = $profileQuery->row_array();
			   		$borrower['Profile'] = $profile;
				}
				$loan['Borrower'] = $borrower;
        	}

        	$ret = $loan;
		}
		return $ret;
	}
    public function getBusiness($id)
	{
		$ret = array();
        // get all entries from loans table (it can be limit down to number of records to retrieve)
		$this->db->select('borrower_id,id')->from('loans')
				 ->where('id', $id);
		if ($loanQuery = $this->db->get()) {
			$loan = $loanQuery->row_array();

			// get all entries from borrowers table (it can be limit down to number of records to retrieve)
			$this->db->select('id,profile_id')->from('borrowers')
				 ->where('id', $loan['borrower_id']);
			if ($borrowerQuery = $this->db->get()) {
				$borrower = $borrowerQuery->row_array();

            	// get all entries from Profile table (it can be limit down to number of records to retrieve)
		    	$this->db->select('*')->from('businesses')
					 ->where('borrower_id', $borrower['id']);
		    	if ($businessQuery = $this->db->get()) {
                	foreach ($businessQuery->result_array() as $row) {
                    	$loan['Business'][] = $row;
                	}
				}
                // get all entries from Profile table (it can be limit down to number of records to retrieve)
		    	$this->db->select('last_name,first_name')->from('profiles')
					 ->where('id', $borrower['profile_id']);
		    	if ($profileQuery = $this->db->get()) {
			   		$profile = $profileQuery->row_array();
			   		$borrower['Profile'] = $profile;
				}

				$loan['Borrower'] = $borrower;
        	}

        	$ret = $loan;
		}
		return $ret;
	}

    public function editBusiness($id)
	{
		$ret = array();
        // get entries from business table (it can be limit down to number of records to retrieve)
		$this->db->select('*')->from('businesses')
				 ->where('id', $id);
		if ($businessQuery = $this->db->get()) {
			$business = $businessQuery->row_array();

            // get all entries from loans table (it can be limit down to number of records to retrieve)
			$this->db->select('id')->from('loans')
				->where('borrower_id', $business['borrower_id']);
			if ($loanQuery = $this->db->get()) {
				$business['loan'] = $loanQuery->row_array();
			}

			// get all entries from borrowers table (it can be limit down to number of records to retrieve)
			$this->db->select('id,profile_id')->from('borrowers')
				 ->where('id', $business['borrower_id']);
			if ($borrowerQuery = $this->db->get()) {
				$borrower = $borrowerQuery->row_array();

            	// get entries from Profile table (it can be limit down to number of records to retrieve)
		    	$this->db->select('last_name,first_name')->from('profiles')
					 ->where('id', $borrower['profile_id']);
		    	if ($profileQuery = $this->db->get()) {
			   		$profile = $profileQuery->row_array();
			   		$borrower['Profile'] = $profile;
				}

				$business['Borrower'] = $borrower;
        	}

        	$ret = $business;
		}
		return $ret;
	}

    public function getNotes($id)
	{
		$ret = array();
        // get all entries from loans table (it can be limit down to number of records to retrieve)
		$this->db->select('borrower_id')->from('loans')
				 ->where('id', $id);
		if ($loanQuery = $this->db->get()) {
			$loan = $loanQuery->row_array();

            // get all entries from notes table (it can be limit down to number of records to retrieve)
		    $this->db->select('*')->from('notes')
				 ->where('borrower_id', $loan['borrower_id']);
		    if ($noteQuery = $this->db->get()) {
               	foreach ($noteQuery->result_array() as $row) {
                   	$loan['Notes'][] = $row;
               	}
			}

            // get all entries from borrowers table (it can be limit down to number of records to retrieve)
			$this->db->select('id,profile_id')->from('borrowers')
				 ->where('id', $loan['borrower_id']);
			if ($borrowerQuery = $this->db->get()) {
				$borrower = $borrowerQuery->row_array();

            // get all entries from Profile table (it can be limit down to number of records to retrieve)
		    	$this->db->select('last_name,first_name')->from('profiles')
					 ->where('id', $borrower['profile_id']);
		    	if ($profileQuery = $this->db->get()) {
			   		$profile = $profileQuery->row_array();
			   		$borrower['Profile'] = $profile;
				}      
    
				$loan['Borrower'] = $borrower;
   			}
        	$ret = $loan;
		}
		return $ret;
	}

	public function editNotes($id)
	{
		$ret = array();
        // get all entries from notes table (it can be limit down to number of records to retrieve)
		$this->db->select('*')->from('notes')
			->where('id', $id);
		if ($noteQuery = $this->db->get()) {
        	$note = $noteQuery->row_array();
		}

        // get all entries from loans table (it can be limit down to number of records to retrieve)
		$this->db->select('id')->from('loans')
			->where('borrower_id', $note['borrower_id']);
		if ($loanQuery = $this->db->get()) {
			$note['loan'] = $loanQuery->row_array();

			$this->db->select('profile_id')->from('borrowers')
				 ->where('id', $note['borrower_id']);
			if ($borrowerQuery = $this->db->get()) {
				$borrower = $borrowerQuery->row_array();

				// get profile information
				$this->db->select('last_name,first_name')->from('profiles')
					 ->where('id', $borrower['profile_id']);
				if ($profileQuery = $this->db->get()) {
				  	$profile = $profileQuery->row_array();
					$borrower['Profile'] = $profile;
				}
				$note['Borrower'] = $borrower;
            }
        	$ret = $note;
		}
		return $ret;
	}


}